const webpack = require('webpack')
const merge = require('webpack-merge')
const base = require('./webpack.base')

const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");
const smp = new SpeedMeasurePlugin();
const TerserPlugin = require('terser-webpack-plugin');
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CompressionPlugin = require('compression-webpack-plugin')

console.log('######################-PROD-PARAM-######################')
console.log('npm_config_speedmeasure--------', process.env.npm_config_speedmeasure)
console.log('######################-PROD-PARAM-######################')

const config = {
  mode: 'production',
  plugins: [
    new HtmlWebpackExternalsPlugin({
      externals: [
        {
          module: 'react',
          entry: 'https://11.url.cn/now/lib/16.2.0/react.min.js',
          global: 'React',
        },
        // {
        //   module: 'react-dom',
        //   entry: 'https://11.url.cn/now/lib/16.2.0/react-dom.min.js',
        //   global: 'ReactDOM',
        // },
      ]
    }),
  ],

  optimization: {
    // minimize: false,//取消内置压缩
    // minimizer: [
    //   new TerserPlugin({ // 压缩js
    //     cache: true,
    //     parallel: true,
    //     sourceMap: true
    //   }),
    // ], //指定mode: 'production'可以不用配置混淆压缩js
    runtimeChunk: {
      name: entrypoint => `runtime-${entrypoint.name}`//从bundle里的runtime提出来，因为就业务代码常常变化，这样可以利用强缓存缓存住无变化chunk
    },
    splitChunks: {
      chunks: "all",
      name: true,
      cacheGroups: {
        vendors: {
          //这个是把第三方库提出来
          test: /node_modules[\\/]/,
          name: "vendors",
          chunks: "all",
          minChunks: 1,
          priority: -10,
        },
        universalComponent: {
          //这个是把通用组件提出来
          chunks: 'all',
          test: /src[\\/]components/,
          minChunks: 1,
          priority: -20,
        },
        otherCommon: {
          //这个是防止重复打包
          chunks: 'all',
          test: /src[\\/]/,
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true
        }
        // common: {
        //   //这个是多入口情况下打出公共依赖的模块（单入口无效的） minChunks：2意味着至少被2个模块引用才提出来
        //   name: "common",
        //   chunks: "initial",
        //   minChunks: 2,
        // },
        // styles: {
        //   name: "styles",
        //   test: /\.css$/,
        //   chunks: "all",
        //   enforce: true
        // }
      }
    }
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'css/[name].[contenthash:8].css',
    }),
    new OptimizeCSSAssetsPlugin({
      assetNameRegExp: /\.css$/g,
      cssProcessor: require('cssnano')
    }),
    new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn|zh-hk|en/),
    new CompressionPlugin({
      test: /\.(js|css|html|svg|mp3|ico)$/, // 针对性压缩
      minRatio: Number.MAX_SAFE_INTEGER
    }),
  ],
}

process.env.npm_config_speedmeasure ? module.exports = smp.wrap(merge(base, config)) : module.exports = merge(base, config)
